<?php

namespace Admin\Controller;

use Admin\Model\ReturnOrderModel;

class ReturnOrderController extends CommonController
{

    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '订单';
        $this->breadcrumb2 = '退换货';
    }

    public function index()
    {

        $model = new ReturnOrderModel();

        $filter = I('get.');

        $search = array();

        if (isset($filter['order_num'])) {
            $search['order_num'] = $filter['order_num'];

        }
        if (isset($filter['user_name'])) {
            $search['user_name'] = $filter['user_name'];

        }
        if (isset($filter['status'])) {
            $search['status'] = $filter['status'];
            $this->get_status = $search['status'];
        }

        $data = $model->show_order_page($search);

        foreach ($data['list'] as $k => $v) {
            $data['list'][$k]['status_text'] = order_stute_text($v['status']);
        }
        $this->assign('empty', $data['empty']);// 赋值数据集
        $this->assign('list', $data['list']);// 赋值数据集
        $this->assign('page', $data['page']);// 赋值分页输出

        $this->status = M('order_status')->select();

        $this->display();
    }

    function print_order()
    {
        $model = new ReturnOrderModel();

        $this->order = $model->order_info(I('id'));
        $this->print = true;
        $this->display('./Themes/Home/default/Mail/order.html');
    }

    public function show_order()
    {

        $this->crumbs = '订单详情';

        $model = new ReturnOrderModel();

        $this->data = $model->order_info(I('id'));
        $data = $this->data;
        if ($data['order']['type'] == 2) {
            //查询快递公司名称
            $this->express = M('express')->select();
        }


        $this->display('show');
    }

    function audit()
    {
        $model = new ReturnOrderModel();
        if (IS_POST) {
            //查询售后单号信息
            $id = I('id');
            $status = I('status');
            if ($status == '0') {
                json_data('', '', 0);
            }
            $where = array(
                'id' => $id,
            );
            $sh = M('order_return_goods')->where($where)->find();

            if ($sh['status'] == $status) {
                json_data('', '', 3);
            }
            if ($sh['status'] == '8') {
                json_data('', '', 1);
            } elseif ($sh['status'] == '12') {
                json_data('', '', 2);
            }


            $result = $model->addReturn($sh, $_POST);
            //$result = true;
            if ($result) {
                //查询订单信息
                $o_where = array(
                    'order_id' => $sh['order_id']
                );
                $order = M('order')->where($o_where)->find();

                $payment_code = $order['payment_code'];
                //审核通过
                if ($status == '9') {
                    //发送短信
                    $a_where = array(
                        'address_id' => $order['address_id'],
                    );
                    $address = M('address')->where($a_where)->find();

                    //调用java的短信发送接口   3des加密手机号码
                    Vendor('Encrypt.Encrypt');
                    $des = new \Encrypt();
                    $e_str = $des->encrypt($order['shipping_tel']);
                    $url =C(SEND_SMS);
                    $param['mobile'] = $e_str;
                    $param['MessageText'] ='您的售后申请已通过，为避免商品超期不能办理退换货，请您尽快寄送商品并上传快递单，如有疑问请致电壹贰钱包。';
                    request_post($url,$param);

                }

                //确认退款  type = 1  退货退款
                if ($sh['type'] == '1') {
                    if ($status == '12') {
                        if ($payment_code == 'installments') {
                            //分期付
                            //请求java接口
                        } else {
                            //一次性支付
                            //原路退款
                            $this->refund($sh['order_num']);

                            //调用java的短信发送接口   3des加密手机号码
                            Vendor('Encrypt.Encrypt');
                            $des = new \Encrypt();
                            $e_str = $des->encrypt($order['shipping_tel']);
                            $url =C(SEND_SMS);
                            $param['mobile'] = $e_str;
                            $on = $order['order_num_alias'];
                            $on_total = $order['total'];
                            $param['MessageText'] ='您好！您的订单:'.$on.'退款已成功受理，'.$on_total.'元将返还到您原支付账户，请在1-3个工作日内注意查收，如有疑问请致电壹贰钱包';
                            request_post($url,$param);
                        }
                    }
                }


                storage_user_action(session('user_auth.uid'), session('user_auth.username'), C('BACKEND_USER'), '审核了售后单：  ' . $sh['return_num']);
                json_data();

            } else {
                json_error();
            }
        }
    }

    function del()
    {
        $model = new ReturnOrderModel();
        $return = $model->del_order(I('get.id'));
        $this->osc_alert($return);
    }

    function stag()
    {
        $order_id = I('get.id');
        //$model= M("Order");

        $sql = "select * from oscshop_staging where id = $order_id";
        $arr = M()->query($sql);
        $this->assign('arr', $arr);
        $this->display();
    }


    //聚合支付——退款
    public function refund($order_no)
    {
        $data_r['order_no'] = $order_no;

        //查询订单信息
        $where = array(
            'order_num_alias' => $data_r['order_no'],
        );

        $order_arr = M('order')->where($where)->find();

        //引进聚合支付的文件——路径
        $path = APP_PATH . '/Lib/pingpp-php/init.php';
        require_once($path);

        //聚合支付的价格需要 * 100
        $data_r['amount'] = $order_arr['total'] * 100;

        //聚合支付的api_key
        $ApiKey = C('API_KEY');
        \Pingpp\Pingpp::setApiKey($ApiKey);

        // Ping++ 流水号	ch_9i9uf1PWX10SDGejTSqj58i9
        $ch_id = $order_arr['paying_id'];
        $ch = \Pingpp\Charge::retrieve($ch_id);

        $ch->refunds->create(
            array(
                'amount' => $data_r['amount'],
                'description' => '退款'
            )
        );

        $re = json_encode($ch);
        if ($re['order_no']) {
            $r_where = array(
                'order_num' => $re['order_no'],
            );
            $up = array(
                'status' => '12',
                'update_time' => time(),
            );
            M('order_return_goods')->where($r_where)->save($up);
        }

    }

}

?>